//
//  YJDSegBtn.m
//  YJDSDK
//
//  Created by tucici on 2017/12/5.
//  Copyright © 2017年 SKL. All rights reserved.
//

#import "YJDSegBtn.h"
@interface YJDSegBtn ()
@property (nonatomic, strong) YJDImageView *iconIMG;
@property (nonatomic, strong) YJDLabel *label;
@property (nonatomic, strong) YJDImageView *selectIMG;
@property (nonatomic, strong) UIView *line;
@end
@implementation YJDSegBtn
- (instancetype)initWithTitle:(NSString*)title icon:(NSString *)icon
{
    self = [super init];
    if (self)
    {
        NSString *header = [[YJDPlatform platform] serviceHeader];
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",header,icon]];
        self.iconIMG.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
        self.label.text = title;
        self.selectIMG.hidden = NO;
        self.line.hidden = NO;
    }
    return self;
}

- (YJDImageView *)iconIMG
{
    if (!_iconIMG)
    {
        _iconIMG = [[YJDImageView alloc]init];
        [self addSubview:_iconIMG];
        [_iconIMG mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(self).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(20, 20));
        }];
    }
    return _iconIMG;
}

- (YJDImageView *)selectIMG
{
    if (!_selectIMG)
    {
        _selectIMG = [[YJDImageView alloc]initWithImageName:@"unselect"];
        _selectIMG.backgroundColor = [UIColor clearColor];
        _selectIMG.layer.masksToBounds = YES;
        _selectIMG.layer.cornerRadius = 15 / 2.0;
        [self addSubview:_selectIMG];
        [_selectIMG mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.right.equalTo(self).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(15, 15));
        }];
    }
    return _selectIMG;
}

- (YJDLabel *)label
{
    if (!_label)
    {
        _label = [[YJDLabel alloc]init];
        _label.textColor = [UIColor blackColor];
        _label.font = [UIFont systemFontOfSize:15 weight:UIFontWeightLight];
        [self addSubview:_label];
        [_label mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(_iconIMG.mas_right).with.offset(10);
        }];
    }
    return _label;
}

- (UIView *)line
{
    if (!_line)
    {
        _line = [[UIView alloc]init];
        _line.backgroundColor = RGBA(223, 223, 223, 1.0);
        [self addSubview:_line];
        [_line mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.bottom.equalTo(self);
            make.height.mas_equalTo(1);
        }];
    }
    return _line;
}

- (void)setSelected:(BOOL)selected
{
    [super setSelected:selected];
    self.selectIMG.imageName = (selected)?@"selected":@"unselect";
    self.selectIMG.backgroundColor = (selected)?[YJDHelper shareHelp].color:[UIColor clearColor];
}
@end

